package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/class/constant"
	"github.com/lab-online/internal/class/entity"
	"github.com/lab-online/internal/shared/valueobject"
	"github.com/lab-online/pkg/auth"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
	"github.com/lab-online/pkg/util"
)

type getClassStudentsRespData []studentInfo

type GetClassStudentRespBody struct {
	Data getClassStudentsRespData `json:"data" binding:"required"`
	resp.BaseRespBody
}

// GetClassStudents 查询班级学生
//
//	@Summary				查询班级学生
//	@Description.markdown	GetClassStudents
//	@Tags					class
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string	true	"`teacher` | `admin`"
//	@Param					class			path		string	true	"班级ID"
//	@Success				200				{object}	GetClassStudentRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Failure				403				{object}	resp.FailedRespBodyString
//	@Failure				500				{object}	resp.FailedRespBodyString
//	@Router					/v1/classes/:id/students [get]
func (h *Handler) GetClassStudents(c *gin.Context) {
	classID := c.MustGet(mw.KeyParams).(*GetClassReqParam).ClassID
	authEntity := c.MustGet(auth.KeyAuth).(auth.AuthEntity)

	classEntity := entity.NewClassEntity(entity.WithID(classID))

	if class, err := h.domain.GetClassStudent(classEntity, authEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeGetClassSuccess, util.SliceMap(class.GetStudentVOs(),
			func(student valueobject.UserVO) studentInfo {
				return studentInfo{
					Name:     student.GetName(),
					Username: student.GetUsername(),
					Role:     student.GetRole(),
				}
			},
		))
	}
}
